package com.faasadmin.faas.modules.admin.app.common;

import cn.hutool.core.io.IoUtil;
import com.faasadmin.faas.business.core.module.system.service.file.SysFileBussService;
import com.faasadmin.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static com.faasadmin.framework.common.pojo.CommonResult.success;

/**
 * @author: faasadmin
 * @description: 通用控制器
 * @data: 2022-09-23 9:44
 **/
@Tag(name = "通用")
@RestController
@RequestMapping("/app/common")
@Validated
public class AppCommonController {

    @Resource
    private SysFileBussService sysFileBussService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    @Parameters({
            @Parameter(name = "file", description = "文件附件", required = true),
            @Parameter(name = "path", description = "文件路径")
    })
    public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
                                           @RequestParam(value = "path", required = false) String path) throws Exception {
        return success(sysFileBussService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())).getUrl());
    }
}
